package com.aidong.myprofile.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aidong.food.dao.TrnFoodBuyDao;
import com.aidong.myprofile.biz.MyAppointmentBiz;
import com.aidong.myprofile.dao.MyAppointmentDao;
import com.aidong.pay.dao.OrderManagementDao;
import com.aidong.stadium.dao.MstScheduleDao;

@Service("appointmentBiz")
public class MyAppointmentBizImpl implements MyAppointmentBiz {
	
	private MyAppointmentDao myAppointmentDao;
	
	private TrnFoodBuyDao trnFoodBuyDao;
	private OrderManagementDao orderManagementDao;

	@Autowired
	public void setOrderManagementDao(OrderManagementDao orderManagementDao) {
		this.orderManagementDao = orderManagementDao;
	}

	@Autowired
	public void setTrnFoodBuyDao(TrnFoodBuyDao trnFoodBuyDao) {
		this.trnFoodBuyDao = trnFoodBuyDao;
	}

	@Autowired
	public void setMyAppointmentDao(MyAppointmentDao myAppointmentDao) {
		this.myAppointmentDao = myAppointmentDao;
	}

	/**
	 * 场馆预约列表
	 * @param idongId
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getStadiumList(Map<String, Object> map) {
		// 预约标记位
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("status", 1);
		paraMap.put("idongId", map.get("idongId"));
		myAppointmentDao.eidtMemberAppointFlg(paraMap);
		return myAppointmentDao.getStadiumList(map);
	}

	/**
	 * 团操课程预约列表
	 * @param idongId
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getCourseList(Map<String, Object> map) {
		return myAppointmentDao.getCourseList(map);
	}

	/**
	 * 私教预约列表
	 * @param idongId
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getCoachList(Map<String, Object> map) {
		return myAppointmentDao.getCoachList(map);
	}

	/**
	 * 活动报名列表
	 * @param idongId
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getCampaignList(Map<String, Object> map) {
		return myAppointmentDao.getCampaignList(map);
	}

	/**
	 * 场馆预约取消
	 * @param map
	 */
	@Override
	public void editStadiuCancle(Map<String, Object> map) {
		myAppointmentDao.editStadiuCancle(map);
	}

	/**
	 * 课程预约取消
	 * @param map
	 */
	@Override
	public void editCourseCancle(Map<String, Object> map) {
		myAppointmentDao.editCourseCancle(map);
		
		map.put("orderType",2);
		//根据预约号获取活动的订单号
		Map<String,Object> resultMap=myAppointmentDao.getCampaginBySubscribeId(map);
		if(resultMap != null){
			// 改电影券改为未使用
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("orderId", resultMap.get("orderId"));
			int payType=Integer.parseInt(resultMap.get("payType").toString());
			orderManagementDao.setMovieTicketsUnuse(paraMap);
			// 代金券改为未使用
			orderManagementDao.setCouponsTicketsUnuse(paraMap);
			// 设置折扣券为未使用
			if (payType == 7 || payType == 8) {
				orderManagementDao.setDiscountTicketsUnuse(paraMap);
			}
		}
	}

	/**
	 * 私教预约取消
	 * @param map
	 */
	@Override
	public void editCoachCancle(Map<String, Object> map) {
		myAppointmentDao.editCoachCancle(map);
	}

	/**
	 * 活动报名取消
	 * @param map
	 */
	@Override
	public void editCampaignCancle(Map<String, Object> map) {
		myAppointmentDao.editCampaignCancle(map);
		myAppointmentDao.editCampaignSignUpCount(Integer.parseInt(map.get("subscribeId") + ""));
		
		map.put("orderType",1);
		//根据预约号获取活动的订单号
		Map<String,Object> resultMap=myAppointmentDao.getCampaginBySubscribeId(map);
		if(resultMap != null){
			try{
				String carnivalSubscribeIds=resultMap.get("carnivalSubscribeIds").toString();
				String[] subIds=carnivalSubscribeIds.split(",");
				if(subIds.length>0){
					// 取消预约
					Map<String, Object> appointMap = new HashMap<String, Object>();
					for(int i=0;i<subIds.length;i++){
						appointMap.clear();
						appointMap.put("applyType", 1);
						appointMap.put("status", 1);
						appointMap.put("subscribeId", subIds[i]);
						int updateResult = orderManagementDao.editAppointStatus(appointMap);
						if (updateResult > 0) {
							orderManagementDao.editCarnivalCampaignIsFull(Integer.valueOf(subIds[i]));
						}
					}
				}
			
			}catch(Exception e){
			}
			// 改电影券改为未使用
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("orderId", resultMap.get("orderId"));
			int payType=Integer.parseInt(resultMap.get("payType").toString());
			orderManagementDao.setMovieTicketsUnuse(paraMap);
			// 代金券改为未使用
			orderManagementDao.setCouponsTicketsUnuse(paraMap);
			// 设置折扣券为未使用
			if (payType == 7 || payType == 8) {
				orderManagementDao.setDiscountTicketsUnuse(paraMap);
			}
		}
	}

	/**
	 * 餐饮订购列表
	 * @param map
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getFoodMealList(Map<String, Object> map) {
		// 修改提醒标记位
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("status", 1);
		paraMap.put("idongId", map.get("idongId"));
		trnFoodBuyDao.editBuyFoodFlg(paraMap);
		return myAppointmentDao.getFoodMealList(map);
	}

	/**
	 * 餐饮购买取消
	 * @param map
	 */
	@Override
	public void editFoodCancle(Map<String, Object> map) {
		myAppointmentDao.editFoodCancle(map);
	}

	@Override
	public List<Map<String, Object>> getCourseOrderList(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return myAppointmentDao.getCourseList(map);
	}
}
